package com.chenken;

/**
 * 反转一个字符串
 * 例如 abcdefgh 输出为hgfedcba
 * 使用头尾双指针，头尾互换，直到头指针>=尾指针，退出循环
 */
public class ReversalString {

	public static void main(String[] args) {
		System.out.println(reversalString("abcdefghijklmnopqrstuvwxyz"));
	}
	
	public static String reversalString(String str) {
		if(str == null || "".equals(str)) return str;
		char[] charArray = str.toCharArray();
		int firstIndex = 0;
		int lastIndex = charArray.length-1;
		while(firstIndex < lastIndex) {
			char tmp = charArray[firstIndex];
			charArray[firstIndex++] = charArray[lastIndex];
			charArray[lastIndex--] = tmp;
		}
		return String.valueOf(charArray);
	}
	
}
